//
// CFileWriterFilter.cpp
//

#include "stdafx.h"
#include "CFileWriterFilter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////////////
CFileWriterFilter::CFileWriterFilter(IGraphBuilder * inGraph) : 
CDXFilter(inGraph, CLSID_FileWriter, "File Writer")
{
	mSink = NULL;
}

CFileWriterFilter::~CFileWriterFilter(void)
{
	ReleaseFilter();
}

IPin * CFileWriterFilter::GetInputPin(void)
{
	return CDXFilter::GetPin(TRUE);
}

BOOL CFileWriterFilter::CreateFilter(void)
{
	if (CDXFilter::CreateFilter())
	{
		HRESULT hr = mFilter->QueryInterface(IID_IFileSinkFilter, 
			(void**) &mSink);
		return SUCCEEDED(hr);
	}
	return FALSE;
}

void CFileWriterFilter::ReleaseFilter(void)
{
	if (mSink)
	{
		mSink->Release();
		mSink = NULL;
	}

	CDXFilter::ReleaseFilter();
}

void CFileWriterFilter::SetDestFile(const char * inFile)
{
	WCHAR	filePath[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, inFile, -1, filePath, MAX_PATH);
	
	if (mSink)
	{
		CMediaType mt;
		LPOLESTR oldFileName = NULL;
		if (SUCCEEDED(mSink->GetCurFile(&oldFileName, &mt)))
		{
			CoTaskMemFree(oldFileName);
			mSink->SetFileName(filePath, &mt);
		}
	}
}

CString CFileWriterFilter::GetDestFile(void)
{
	if (mSink)
	{
		CMediaType mt;
		WCHAR * wFilePath = NULL;
		HRESULT hr = mSink->GetCurFile(&wFilePath, &mt);
		if (SUCCEEDED(hr))
		{
			char filePath[MAX_PATH];
			WideCharToMultiByte(CP_ACP, 0, wFilePath, -1, filePath, 
				MAX_PATH, 0, 0);
			CoTaskMemFree(wFilePath);
			return filePath;
		}	
	}
	return "";
}
